﻿namespace SampleApp
{
	using System;
	using System.Collections.Generic;
	using System.Diagnostics;
	using System.IO;
	using System.Linq;
	using System.Reflection;
	using System.Text;
	using System.Threading;

	using PdfGeneratorNetFree.PdfContentItem;
	using PdfGeneratorNetFree.PdfXmlParser;

	class Program
	{
		static void Main(string[] args)
		{
			var templateFile = string.Empty;
			var dataFile = string.Empty;

			for (var i = 0; i < args.Length; i++)
			{
				Console.WriteLine("引数" + i + " = " + args[i]);
				if (i == 0)
				{
					templateFile = args[i];
				}
				else if (i == 1)
				{
					dataFile = args[i];
				}
			}

			Console.WriteLine(string.Empty);
			Console.WriteLine(string.Empty);

			if (string.IsNullOrEmpty(templateFile))
			{
				var assemblyName = Assembly.GetExecutingAssembly().GetName().Name;
				Console.WriteLine("引数を正しく指定してください。\r\n");
				Console.WriteLine("　" + assemblyName + ".exe  Template.xml  [Data.csv]");
				Console.WriteLine("　※Data.csvの指定がない時は空のTextItemにItem名を出力します。");
				Thread.Sleep(5000);
				return;
			}

			CreatePdf(templateFile, dataFile);
		}

		private static void CreatePdf(string templateFile, string dataFile)
		{
			if (string.IsNullOrEmpty(dataFile))
			{
				WritePdf(templateFile);
				return;
			}

			var csvData = File.ReadAllLines(dataFile, Encoding.UTF8);
			var header = csvData[0].Split(',');

			for (var i = 1; i < csvData.Length; i++)
			{
				var values = csvData[i].Split(',');
				WritePdf(templateFile, header, values);
			}
		}


		private static void WritePdf(string templateFile, string[] itemNames = null, string[] itemValues = null)
		{
			var parser = new PdfXmlParser(templateFile);
			var order = parser.PdfDoc;
			var dataContent = parser.ContentDic["data"];

			string outputFile;

			if (itemNames == null)
			{
				//outputFile = "TemplateFile_" + DateTime.Now.ToString("HHmm-ssfff") + ".pdf";
				outputFile = "TemplateFile.pdf";
				SetItemName(dataContent.ItemDic);
			}
			else
			{
				SetContentData(itemNames, itemValues, out outputFile, dataContent.ItemDic);
			}

			order.AddContent("page1", dataContent.Items);
			order.Finish();

			File.WriteAllBytes(outputFile, order.GetBinary());
		}

		private static void SetContentData(string[] itemNames, string[] itemValues, out string outputFile, Dictionary<string, AbstractPdfContentItem> contentItemDic)
		{
			outputFile = "default.pdf";
			for (var i = 0; i < itemNames.Length; i++)
			{
				if (contentItemDic.Keys.Contains(itemNames[i]))
				{
					((Text)contentItemDic[itemNames[i]]).Str = itemValues[i];
				}

				if (itemNames[i] == "outputFileName")
				{
					outputFile = itemValues[i];
				}
			}
		}

		private static void SetItemName(Dictionary<string, AbstractPdfContentItem> contentItemDic)
		{
			foreach (var item in contentItemDic)
			{
				var textItem = item.Value as Text;
				if ((textItem != null) && string.IsNullOrEmpty(textItem.Str))
				{
					textItem.Str = item.Key;
				}
			}
		}
	} //class
}
